package com.tomdd.controller;

import com.tomdd.model.resp.Result;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zx
 * @date 2024-07-20 08:11
 */
@RequestMapping("/test")
@RestController
@CrossOrigin
public class TestController {
    private static final List<Article> articles = new ArrayList<>();

    @GetMapping("/testInfo")
    public Result testInfo() {
        System.out.println("xxxx");
        return Result.success("测试访问接口成功");
    }

    @GetMapping("/article/search")
    public Result<?> searchArticleByCategoryOrState(@RequestParam String category, @RequestParam String state) {
        if (StringUtils.isEmpty(category) && StringUtils.isEmpty(state)) {
            return Result.success(articles);
        }
        List<Article> newArticleList = articles.stream().filter(m -> m.getCategory().equals(category) || m.getState().equals(state))
                .collect(Collectors.toList());

        return Result.success(newArticleList);
    }
    @PostMapping("/article/search2")
    public Result<?> searchArticleByCategoryOrState2(@RequestBody Article article) {
        String category = article.getCategory();
        String state = article.getState();
        if (StringUtils.isEmpty(category) && StringUtils.isEmpty(state)) {
            return Result.success(articles);
        }
        List<Article> newArticleList = articles.stream().filter(m -> m.getCategory().equals(category) || m.getState().equals(state))
                .collect(Collectors.toList());

        return Result.success(newArticleList);
    }




    @GetMapping("/article/getAll")
    public Result<?> getArticleList() {
        return Result.success(articles);
    }


    static {
        Article article1 = new Article("测试标题1", "测试分类", "2024-07-20", "草稿");
        Article article2 = new Article("测试标题2", "测试分类", "2024-07-20", "发布");
        Article article3 = new Article("测试标题3", "测试分类", "2024-07-20", "发布");
        Article article4 = new Article("测试标题4", "测试分类", "2024-07-20", "发布");

        articles.add(article1);
        articles.add(article2);
        articles.add(article3);
        articles.add(article4);
    }


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Article {
        private String title;
        private String category;

        private String date;

        private String state;
    }
}
